package cz.muni.fi.pa165.rentalofconstructionmachineryweb.controller;

import cz.muni.fi.pa165.rentalofconstructionmachinery.CustomerType;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.CustomerDTO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.dto.RentalDTO;
import cz.muni.fi.pa165.rentalofconstructionmachinery.service.CustomerService;
import cz.muni.fi.pa165.rentalofconstructionmachinery.service.RentalService;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/customer")
public class CustomerController {
    
    private CustomerService customerService;
    private RentalService rentalService;

    @RequestMapping("")
    public String list(Model model) {
        model.addAttribute("customers", customerService.getAllCustomers());
        return "customer";
    }
    
    @RequestMapping(value = "/{id}/details", method = RequestMethod.GET)
    public String getDetails(@PathVariable Long id, Model model) {
        if(null == id) {
            return "redirect:/customer?error&msg=Customer Id not provided";
        } else {
            CustomerDTO customer = customerService.getCustomerById(id);
            if(null == customer) {
                return "redirect:/customer?error&msg=Customer with given Id doesn't exist";
            } else {
                model.addAttribute("customer", customer);

                List<RentalDTO> rentals = rentalService.getRentalsByCustomer(customer);
                model.addAttribute("rentals", rentals);

                return "customerDetails";
            }
        }
    }
    
    @RequestMapping(value = "/new", method = RequestMethod.GET)
    public String getNew(Model model) {
        List<String> types = new ArrayList<>();
        for (CustomerType type : CustomerType.values()) {
            types.add(type.type());
        }
        model.addAttribute("types", types);
        
        return "customerForm";
    }
    
    @RequestMapping(value = "/new", method = RequestMethod.POST)
    public String postNew(@RequestParam("name") String name, @RequestParam("type") String type,
        @RequestParam("username") String username, @RequestParam("password") String password, Model model) {
        if(null == name || 0 == name.length() || null == type || 0 == type.length()) {
            return "redirect:/customer?error&msg=Some fields not correctly populated";
        } else {
            CustomerDTO customer = new CustomerDTO(null, name, CustomerType.fromString(type), username, password, 5);
            customerService.addCustomer(customer);
            return "redirect:/customer?success";
        }
    }
    
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.GET)
    public String getEdit(@PathVariable Long id, Model model) {
        if(null == id) {
            return "redirect:/customer?error&msg=Customer Id not provided";
        } else {
            CustomerDTO customer = customerService.getCustomerById(id);
            if(null == customer) {
                return "redirect:/customer?error&msg=Customer with given Id doesn't exist";
            } else {
                model.addAttribute("name", customer.getName());

                List<String> types = new ArrayList<>();
                for (CustomerType type : CustomerType.values()) {
                    types.add(type.type());
                }
                model.addAttribute("types", types);
                model.addAttribute("username", customer.getUsername());

                return "customerForm";
            }
        }
    }
    
    @RequestMapping(value = "/{id}/edit", method = RequestMethod.POST)
    public String postEdit(@PathVariable Long id, @RequestParam("name") String name,
        @RequestParam("type") String type, @RequestParam("username") String username,
        @RequestParam("password") String password, Model model) {
        if(null == name || 0 == name.length() || null == type || 0 == type.length() || null == id) {
            return "redirect:/customer?error&msg=Some fields not correctly populated";
        } else {
            CustomerDTO customer = customerService.getCustomerById(id);
            if(null == customer) {
                return "redirect:/customer?error&msg=Customer with given Id doesn't exist";
            } else {
                customer.setName(name);
                customer.setType(CustomerType.fromString(type));
                customer.setUsername(username);
                if (!password.equals("")) {
                    customer.setPassword(password);
                }
                customerService.modifyCustomer(customer);
                return "redirect:/customer?success";
            }
        }
    }
    
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.GET)
    public String delete(@PathVariable Long id, Model model) {
        if(null == id) {
            return "redirect:/customer?error&msg=Customer Id not provided";
        } else {
            CustomerDTO customer = customerService.getCustomerById(id);
            if(null == customer) {
                return "redirect:/customer?error&msg=Customer with given Id doesn't exist";
            } else {
                for(RentalDTO rental : rentalService.getRentalsByCustomer(customer)) rentalService.removeRental(rental);
                customerService.removeCustomer(customer);
                return "redirect:/customer?success";
            }
        }
    }
    
    @Autowired
    public CustomerController(CustomerService customerService, RentalService rentalService) {
        this.customerService = customerService;
        this.rentalService = rentalService;
    }
    
}
